/*
 * @Author: lduoduo
 * @Date: 2021-02-05 10:50:41
 * @Last Modified by: lduoduo
 * @Last Modified time: 2021-02-05 10:54:14
 * 事件BUS
 */

const events = {
  fnMap: {},
  on(name, fn) {
    if (!this.fnMap[name]) {
      this.fnMap[name] = [];
    }

    this.fnMap[name].push(fn);
  },
  off(name, fn) {
    if (!this.fnMap[name]) return;
    const i = this.fnMap[name].findIndex((d) => d === fn);

    if (i < 0) return;

    this.fnMap[name].splice(i, 1);
  },
  emit(name, ...e) {
    if (!this.fnMap[name]) return;

    this.fnMap[name].map((d) => d(...e));
  },
};

export default events;
